package sample.servlet;

import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import fri.entiry.FrUser;
import fri.entiry.FrPaymentcode;
import fri.businessLayer.CurrencyBeanRemote;
import fri.businessLayer.PaymentCodeBeanRemote;

public class GetcodeController extends HttpServlet {

    @EJB
    private CurrencyBeanRemote currencyBeanRemote;
    private PaymentCodeBeanRemote paymentCodeBeanRemote;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String action = request.getParameter("action");
            if (action != null) {
                if (action.equals("get code")){
                    String txtToUserId          = request.getParameter("payTo");
                    Float fltValues             = Float.parseFloat(request.getParameter("value"));
                    Integer intCurrencyId       =  Integer.parseInt(request.getParameter("selectVal"));
                    String txtDescription       = request.getParameter("txtDes");
                    HttpSession session = request.getSession(true);
                    FrUser frUser = (FrUser)session.getAttribute("USER_LOGIN");
                    String txtFromUserId        = frUser.getUserId();
                    Integer intStatus           = 0;
                    
                    if (null != paymentCodeBeanRemote.generateCode(txtFromUserId, txtToUserId, intCurrencyId, fltValues, txtDescription, intStatus)) {
                        RequestDispatcher rd = request.getRequestDispatcher("reqHome.jsp");
                        rd.forward(request, response);
                    }
                }
            }
            
            request.setAttribute("lstCurrency", currencyBeanRemote.getAllCurrency());
            RequestDispatcher rd = request.getRequestDispatcher("reqGetcode.jsp");
            rd.forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
